
package com.hlkj.pay.app.merchant.impl;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.common.constants.BaseConstants;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.vo.merchant.req.access.*;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessInfoReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantCardReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantLicenseReq;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/25 13:09
 */
@Slf4j
@RequiredArgsConstructor
public class MerchantAccessParamsCheck {

    /**
     * 参数校验
     * 
     * @param merchantAccessReq
     * @return
     */

    public static CommonResult<Void> checkParams(MerchantAccessReq merchantAccessReq) {
        MerchantAccessInfoReq merchantAccessInfo = merchantAccessReq.getMerchantAccessInfo();
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessInfo.getControllerType())) {
            if (merchantAccessInfo.getMerchantAccessController() == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "控制人信息 不能为空");
            }
        }
        MerchantAccessLicenseReq merchantAccessLicense = merchantAccessInfo.getMerchantAccessLicense();
        MerchantAccessCardReq merchantAccessCard = merchantAccessReq.getMerchantAccessCard();
        MerchantAccessContactReq merchantAccessLegal = merchantAccessInfo.getMerchantAccessLegal();
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessLegal.getCertValidityType())) {
            if (!StringUtils.hasText(merchantAccessLegal.getCertEndDate())) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "法人身份证非长期有效,结束日期不能为空");
            }
        }
        else {
            merchantAccessLegal.setCertEndDate(BaseConstants.MAX_DATA_STRING);
        }
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessInfo.getContactType())) {
            MerchantAccessContactReq merchantAccessContact = merchantAccessInfo.getMerchantAccessContact();
            if (merchantAccessContact == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "联系人信息 不能为空");
            }
            else {
                if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessContact.getCertValidityType())) {
                    if (!StringUtils.hasText(merchantAccessContact.getCertEndDate())) {
                        return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "联系人身份证非长期有效,结束日期不能为空");
                    }
                }
                else {
                    merchantAccessContact.setCertEndDate(BaseConstants.MAX_DATA_STRING);
                }
            }
        }
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessInfo.getControllerType())) {
            MerchantAccessCertReq merchantAccessController = merchantAccessInfo.getMerchantAccessController();
            if (merchantAccessController == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "控制人信息 不能为空");
            }
            else {
                if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessController.getCertValidityType())) {
                    if (!StringUtils.hasText(merchantAccessController.getCertEndDate())) {
                        return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "控制人身份证非长期有效,结束日期不能为空");
                    }
                }
                else {
                    merchantAccessController.setCertEndDate(BaseConstants.MAX_DATA_STRING);
                }
            }
        }
        if (!CollectionUtils.isEmpty(merchantAccessInfo.getMerchantAccessShareholderList())) {
            for (MerchantAccessCertReq merchantAccessCertReq : merchantAccessInfo.getMerchantAccessShareholderList()) {
                if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessCertReq.getCertValidityType())) {
                    if (!StringUtils.hasText(merchantAccessCertReq.getCertEndDate())) {
                        return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "股东身份证非长期有效,结束日期不能为空");
                    }
                }
                else {
                    merchantAccessCertReq.setCertEndDate(BaseConstants.MAX_DATA_STRING);
                }
            }
        }
        if (!CollectionUtils.isEmpty(merchantAccessInfo.getMerchantAccessBenefitList())) {
            for (MerchantAccessCertReq merchantAccessCertReq : merchantAccessInfo.getMerchantAccessBenefitList()) {
                if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessCertReq.getCertValidityType())) {
                    if (!StringUtils.hasText(merchantAccessCertReq.getCertEndDate())) {
                        return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "受益人身份证非长期有效,结束日期不能为空");
                    }
                }
                else {
                    merchantAccessCertReq.setCertEndDate(BaseConstants.MAX_DATA_STRING);
                }
            }
        }
        if (StringUtils.hasText(merchantAccessCard.getCertBeginDate())) {
            if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessCard.getCertValidityType())) {
                if (!StringUtils.hasText(merchantAccessCard.getCertEndDate())) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "持卡人身份证非长期有效,结束日期不能为空");
                }
            }
            else {
                merchantAccessCard.setCertEndDate(BaseConstants.MAX_DATA_STRING);
            }
        }

        switch (MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType())) {
            case COMPANY:
                if (merchantAccessLicense == null) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "类型为企业 营业执照不能为空");
                }
                if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessLicense.getLicenseValidityType())) {
                    if (!StringUtils.hasText(merchantAccessLicense.getLicenseEndDate())) {
                        return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "类型为企业 营业执照非长期有效,结束日期不能为空");
                    }
                }
                else {
                    merchantAccessLicense.setLicenseEndDate(BaseConstants.MAX_DATA_STRING);
                }
                switch (MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType())) {
                    case CORPORATE_ACCOUNT:
                        break;
                    default:
                        // if (!StringUtils.hasText(merchantAccessCard.getOfficialName())) {
                        //     return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡账号名称 不能为空");
                        // }
                        // if (!StringUtils.hasText(merchantAccessCard.getOfficialCardNo())) {
                        //     return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行账号 不能为空");
                        // }
                        // if (!StringUtils.hasText(merchantAccessCard.getOfficialBankCode())) {
                        //     return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行编码 不能为空");
                        // }
                        // if (!StringUtils.hasText(merchantAccessCard.getOfficialBankName())) {
                        //     return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行名称 不能为空");
                        // }
                        // if (!StringUtils.hasText(merchantAccessCard.getOfficialBranchCode())) {
                        //     return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡联行号 不能为空");
                        // }
                }
                break;
        }
        switch (MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType())) {
            case CORPORATE_ACCOUNT:
                if (!StringUtils.hasText(merchantAccessCard.getBankCode())) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "对公账号 银行编码 不能为空");
                }
                if (!StringUtils.hasText(merchantAccessCard.getBranchCode())) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "对公账号 联行号 不能为空");
                }
                break;
            case PRIVATE_NON_CORPORATE_ACCOUNT:
        }
        return CommonResult.success();
    }

    /**
     * 参数校验
     *
     * @param merchantAccessReq
     * @return
     */

    public static CommonResult<Void> checkParams(OpenApiMerchantAccessReq merchantAccessReq) {
        OpenApiMerchantAccessInfoReq merchantAccessInfo = merchantAccessReq.getMerchantAccessInfo();
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantAccessInfo.getControllerType())) {
            if (merchantAccessInfo.getMerchantAccessController() == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "控制人信息 不能为空");
            }
        }
        OpenApiMerchantLicenseReq merchantAccessLicense = merchantAccessInfo.getMerchantAccessLicense();
        OpenApiMerchantCardReq merchantAccessCard = merchantAccessReq.getMerchantAccessCard();
        switch (MerchantEnum.MERCHANT_TYPE.from(merchantAccessInfo.getType())) {
            case COMPANY:
                if (merchantAccessLicense == null) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "类型为企业 营业执照不能为空");
                }
                switch (MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType())) {
                    case CORPORATE_ACCOUNT:
                        break;
                    default:
                        if (!StringUtils.hasText(merchantAccessCard.getOfficialName())) {
                            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡账号名称 不能为空");
                        }
                        if (!StringUtils.hasText(merchantAccessCard.getOfficialCardNo())) {
                            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行账号 不能为空");
                        }
                        if (!StringUtils.hasText(merchantAccessCard.getOfficialBankCode())) {
                            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行编码 不能为空");
                        }
                        if (!StringUtils.hasText(merchantAccessCard.getOfficialBankName())) {
                            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡银行名称 不能为空");
                        }
                        if (!StringUtils.hasText(merchantAccessCard.getOfficialBranchCode())) {
                            return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "企业非对公结算 对公卡联行号 不能为空");
                        }
                }
                break;
        }
        switch (MerchantEnum.CARD_TYPE.from(merchantAccessCard.getType())) {
            case CORPORATE_ACCOUNT:
                if (!StringUtils.hasText(merchantAccessCard.getBankCode())) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "对公账号 银行编码 不能为空");
                }
                if (!StringUtils.hasText(merchantAccessCard.getBranchCode())) {
                    return CommonResult.error(CommonResultCode.PARAMETER_MISSING.getCode(), "对公账号 联行号 不能为空");
                }
                break;
            case PRIVATE_NON_CORPORATE_ACCOUNT:
        }
        return CommonResult.success();
    }
}
